home *** CD-ROM | disk | FTP | other *** search
- Path: newsroom.hitc.com!usenet
- From: psand@eos.hitc.com (G. Patrick Sand)
- Newsgroups: comp.lang.c++
- Subject: Re: [Q] Dynamically allocating memory for a char*
- Date: 19 Jan 1996 18:56:38 GMT
- Organization: Hughes Aircraft (EOSDIS)
- Message-ID: <4doph6$507@newsroom.hitc.com>
- References: <4dmn1i$10t@walrus2.walrus.com> <NITIN.96Jan19095012@more.eng.sun.com>
- NNTP-Posting-Host: 155.157.118.56
- Mime-Version: 1.0
- X-Newsreader: WinVN 0.99.3
-
- [SNIP]
- >In article <4dmn1i$10t@walrus2.walrus.com> fjordao@walrus.com (Felipe
- Jordao)
- >writes:
- >
- >> From: fjordao@walrus.com (Felipe Jordao)
- >> Newsgroups: comp.lang.c++
- >> Date: Fri, 19 Jan 1996 00:00:49 GMT
- >> Organization: HAC
- >>
- >> Hi,
- >>
- >> I have a question about allocating memory for a string as it's passed
- >> in by the user. I'm using cin to get the info, but maybe this is
- >> inappropriate. I have included the relevant snippets of the file
- >> below. What I am trying to achieve is no limitation for the user when
- >> they input a path + filename. Is it possible to use char* foo and
- >> then something like (I know this is wrong, but the idea of it ...)
- >>
- >> cin >> malloc(sizeof(infile)); :0
- >>
- >> Here is the actual code
- >> ----------------------------------------------------------------------
- >>
- [SNIP,SNIP]
-
- How about this:
-
- use the cin.get( char* buf, int length, char term) method
-
- This reads into a buffer (buf) up to length bytes and null-terminates it
- (dropping the last byte if the stream read in has length non-null
- characters), until it hits the term character.
-
- Just pick a buffer size (like 512 or 1024) that you know they have no
- right to exceed and you then manipulate the buffer to get your string...
-
- I found this in section 10.3.1 (p. 332) of The C++ Programming Language
- (2nd Edition) by Stroustrup (ISBN 0-201-53992-6)--mine is copyrighted
- 1991
-
- Of course, a later edition is probably out there; just look in the
- Streams chapter on Output...
-
- Hope this helps...
-
- --
- G. Patrick Sand
- psand@eos.hitc.com
- PatSand@aol.com
- (301) 925-0791
- "Travel Light But Right..."
- Microsoft Network is prohibited from redistributing
- this work in any form, in whole or in part. License
- to distribute this individual post is available to Microsoft
- for $999. Posting without permission constitutes an
- agreement to these terms...gps
-
-